--#include "data\config\boss\LockBossConfig.lua" once
----#include "data\functions\CommonFunc.lua" once


--[[
注册一些事件 for test
]]
function ResisterLockBossProc()
	--print("ResisterLockBossProc...")
	for level, lockBossCfg in ipairs(LockBossConfig) do
		SceneExitDispatcher.register( lockBossCfg.sceneId, OnExitLockBoss )
	end
end

--[[
退出BOSS之家
注意：手工退出、下线、被T出，GM指令Move出都会触发
]]
function OnExitLockBoss( sysarg, sceneId, fubenId )
	CloseActivityRightPanel(sysarg)				--关闭面板
	Actor.removeEntityScriptDelayMsg( sysarg, 17 )		--离开BOSS之家场景，删除延迟消息
end

--[[
自动判断能进入的场景
]]
function EnterLockBoss( sysarg )
	--print("EnterLockBoss...")
	local lockBossCfg = GetLockBossCfg( sysarg )
	if not lockBossCfg then
		return
	end

	if CommonEnterScence( sysarg,  lockBossCfg, GameLog.clLockBossEnterFee, Lang.ScriptTips.LockBossLog01 ) then
		Actor.closeDialogs( sysarg, enLinkLockBossWnd )
		local dDvar = Actor.getDyanmicVar(sysarg)
		dDvar.enterLockBossTime	= System.getCurrMiniTime()		--进入场景的时间

		InitLockBossPanel( sysarg )

		--注册回城的延迟消息
		Actor.removeEntityScriptDelayMsg( sysarg, 17 )
		--[[最后的true表示实体死亡，消息保留]]
		Actor.postEntityScriptDelayMsg( sysarg, lockBossCfg.beInTime*1000, 0, 17,0,0,0,0,false, true )

		System.broadcastTipmsgLimitLev( string.format(Lang.ScriptTips.LockBoss002, Actor.getName( sysarg )), ttScreenCenter + ttChatWindow,
			lockBossCfg.enterLevelMin[2])
	end
end


--boss被刷出
function OnLockBossMob(monster, monId, sceneId, x, y)

end

--boss被击杀
--sysarg:归属玩家
--lastHitKiller：最后一击玩家
function OnLockBossDie(monster, sysarg, monId, lastHitKiller)
	--print("OnLockBossDie, monId="..monId)
	--[[
	local lockBossCfg = GetLockBossCfgByBossId( monId )
	if not lockBossCfg then
		return
	end

	local deadBossCfg = {}
	deadBossCfg.monsterId 	= 	lockBossCfg.deadMonsterId
	deadBossCfg.sceneId		=	lockBossCfg.sceneId
	deadBossCfg.livetime 	=	lockBossCfg.revivalCd
	deadBossCfg.num			=	1
	deadBossCfg.pos 		=	lockBossCfg.pos
	freshSceneMonsterInPos( deadBossCfg )
	]]
end

--墓碑boss生命期到
--sysarg:归属玩家
function OnDeadLockBossLiveTimeOut(monster, monId, sceneId)
	--print("OnDeadLockBossLiveTimeOut, monId="..monId..", sceneId="..sceneId)
	--[[
	local lockBossCfg = GetLockBossCfgByDeadBossId( monId )
	if not lockBossCfg then
		return
	end

	freshSceneMonsterInPos( lockBossCfg )

	local monName 	= System.getMonsterNameById( lockBossCfg.monsterId )
	local sceneName = Fuben.getSceneNameById( lockBossCfg.sceneId )
	local msg 		= string.format( Lang.Script.LockBoss002, monName, sceneName )
	System.broadcastTipmsg( msg, ttScreenCenter + ttChatWindow )
	]]
end

function GetLockBossCfg( sysarg )
	local circle = Actor.getIntProperty(sysarg, PROP_ACTOR_CIRCLE)
	local level  = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
	for i, lockBossCfg in ipairs( LockBossConfig ) do
		if lockBossCfg.enterLevelMin[1] <= circle and circle <= lockBossCfg.enterLevelMax[1] and
		   lockBossCfg.enterLevelMin[2] <= level and level <= lockBossCfg.enterLevelMax[2] then
			return lockBossCfg
		end
	end
	return nil
end

function GetLockBossCfgByBossId( bossId )
	local circle = Actor.getIntProperty(sysarg, PROP_ACTOR_CIRCLE)
	for level, lockBossCfg in ipairs(LockBossConfig) do
		if bossId == lockBossCfg.monsterId then
			return lockBossCfg
		end
	end
	return nil
end

function GetLockBossCfgBySceneId( sceneId )
	for level, lockBossCfg in ipairs(LockBossConfig) do
		if sceneId == lockBossCfg.sceneId then
			return lockBossCfg
		end
	end
	return nil
end

function GetLockBossCfgByDeadBossId( deadBossId )
	local circle = Actor.getIntProperty(sysarg, PROP_ACTOR_CIRCLE)
	for level, lockBossCfg in ipairs(LockBossConfig) do
		if deadBossId == lockBossCfg.deadMonsterId then
			return lockBossCfg
		end
	end
	return nil
end

--[[
初始化右侧面板
]]
function InitLockBossPanel( sysarg )
	local sceneId = Actor.getSceneId( sysarg )
	local lockBossCfg = GetLockBossCfgBySceneId( sceneId )
	if not lockBossCfg then
		return
	end

	local dDvar = Actor.getDyanmicVar(sysarg)
	local activityType 	= 2 			--活动
	local activityId 	= enRightPanelActivity_LockBoss
	local restTime 		= dDvar.enterLockBossTime + lockBossCfg.beInTime - System.getCurrMiniTime()
	if restTime < 0 then
		restTime = 0
	end

	local panelInfo 	= {}
	panelInfo.sts 		= 1  										--进行中
	panelInfo.title 	= Lang.ScriptTips.LockBossName
	panelInfo.timeTitle = Lang.ScriptTips.panelCont01			--剩余时间
	panelInfo.restTime	= restTime


	panelInfo.buttons   	= {}
	panelInfo.buttons[1]	= 1							--1：退出副本，2-单倍领奖，3-双倍领奖

	OpenActivityRightPanel( sysarg, activityType, activityId, panelInfo )
end

--[[
下发BOSS复活时间
]]
function SendLockBossReliveTime( sysarg )
	local npack = DataPack.allocPacket(sysarg, enBossSystemID, sLockBossSendReliveTime)
	if npack then
		DataPack.writeByte(npack, #LockBossConfig)
		for idx, lockBossCfg in ipairs( LockBossConfig ) do
			local bossId = lockBossCfg.bossId[1] or 0
			local reliveTime = GetCommonBossReliveTime( lockBossCfg.sceneId, 0, bossId )
			DataPack.writeByte(npack, idx)
			DataPack.writeUInt(npack, reliveTime)
			--print("SendLockBossReliveTime, idx="..idx..", bossId="..bossId..", reliveTime="..reliveTime)
		end
		DataPack.flush(npack)
	end
end

ResisterLockBossProc()

